#include "../include/my_wifi.hpp"
#include "../include/my_fs.hpp"

static MY_WIFI_CFG cfg;

static void connectWF(void){
    int count = 1;
    WiFi.begin(my_wifi.cfg->name, my_wifi.cfg->passwd);
    Serial.print("连接到wifi [" + my_wifi.cfg->name + "]");
    while (WiFi.status() != WL_CONNECTED) {
        count ++;
        Serial.print(".");
        if(Serial.available()){
            if(Serial.readString() == "del")
                my_fs.ops->clean("/config.json");
        }
        delay(250);
        if (count > 100){
            Serial.println("WIFI连接超时!请检查wifi配置");
            Serial.println("WIFI名["+my_wifi.cfg->name+"]");
            Serial.println("WIFI密码["+my_wifi.cfg->passwd+"]");

            while (Serial.available() == 0){
                Serial.print(".");
                delay(100);
            }
        }
    }

    my_wifi.cfg->ip = WiFi.localIP().toString();
    Serial.println("");
    Serial.println("WIFI已连接");
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    delay(200);
}

static MY_WIFI_OPS ops = {
    .connect = connectWF,
};

MY_WIFI my_wifi = {
    .cfg = &cfg,
    .ops = &ops,
};
